fix: refactor WebSocket client to encapsulate connection lifecycle an…#320
Open
Conversation
…d prevent global scope pollution
codeZe-us
self-requested a review
July 16, 2026 22:19
codeZe-us
approved these changes
Jul 16, 2026
Contributor
|
@okeolaolatun23-glitch please resolve conflicts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #316
✅ Refactoring Complete
I've successfully refactored the WebSocket client to eliminate global scope pollution and enable advanced connection management.
Key Improvements:
Encapsulated Architecture:
TraceStreamClientclass wraps all socket initialization and event listener logicnodeCount,startTime,wsreference) now lives within instance, not globallyopen,message,error,close) are bound cleanly insideattachEventListeners()Graceful Connection Management:
connect()method returns a promise, allowing proper async handlingcleanup()method removes all listeners and nullifies the socket reference—enabling garbage collection of dead socketsclose()method gracefully closes connections before attempting reconnectionisConnecting,isClosed) prevent duplicate connectionsFactory Pattern:
connectToTraceStream()factory function provides a clean API for creating and connecting clientsOrganized Message Handling:
handleMessageType()routes messages to dedicated handler methodstrace_started,trace_node, etc.) has its own handlerThis architecture now supports:
Made changes.